﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Castle.ActiveRecord;
using System.Reflection;
using Castle.ActiveRecord.Framework.Config;
using ARPrimitiveType.Model;
using Castle.ActiveRecord.Queries;
using NHibernate.Criterion;

namespace ARPrimitiveType
{
	class Program
	{
		static void Main(string[] args)
		{
			log4net.Config.XmlConfigurator.Configure();
			ActiveRecordStarter.Initialize(Assembly.GetExecutingAssembly(), ActiveRecordSectionHandler.Instance);
			ActiveRecordStarter.CreateSchema();

			AddCompanies();
			HQLTest();
			CriteriaTest();
		}

		private static void CriteriaTest()
		{
			DetachedCriteria query = DetachedCriteria.For<Company>("Company")
				.Add(Expression.Eq("CompanyType", CompanyType.Manufacturing));

			Company[] results = Company.FindAll(query);
		}

		private static void HQLTest()
		{
			SimpleQuery<Company> query = new SimpleQuery<Company>(
				"from Company c where c.CompanyType = ?", CompanyType.Manufacturing);
			Company[] results = query.Execute();
		}

		private static void AddCompanies()
		{
			Company c = new Company();
			c.Name = "ABC Software";
			c.InceptionDate = new DateTime(2008, 1, 1);
			c.CompanyType = CompanyType.Insurance;
			c.Save();

			c = new Company();
			c.Name = "ACME";
			c.InceptionDate = new DateTime(1956, 3, 6);
			c.CompanyType = CompanyType.Manufacturing;
			c.Save();

			c = new Company();
			c.Name = "Dave's Landscaping";
			c.CompanyType = CompanyType.Manufacturing;
			c.Save();
		}
	}
}
